﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Security;
using ImageHandler;
using MvcPaging;
using Stamp_For_Africa_Version_2.Models;
using Stamp_For_Africa_Version_2.Repositories;
using Stamp_For_Africa_Version_2.ViewModels;

namespace Stamp_For_Africa_Version_2.Controllers
{
	public class MyAlbumsController : Controller
	{

		private Guid GetUserId()
		{
			MembershipUser user = Membership.GetUser();
			Guid userId = Guid.Empty;
			if (user != null)
				userId = (Guid)user.ProviderUserKey;

			return userId;
		}

		//
		// GET: /MyAlbums/
		[Authorize]
		public ActionResult Index()
		{
			Response.Cache.SetNoStore();
			

			AlbumRepository albumRepo = new AlbumRepository();
			List<Album> myAlbums = albumRepo.FindAllAlbums(GetUserId()).ToList();
			ViewBag.AlbumList = myAlbums;
			return View();
		}

		[Authorize]
		public ActionResult Create()
		{
			return View();
		}

		[Authorize]
		public ActionResult Details(Guid id)
		{
			AlbumRepository albumRepository = new AlbumRepository();
			CollectionRepository collectionRepository = new CollectionRepository();
      StampRepository stampRepository = new StampRepository();

      Album album = albumRepository.GetAlbum(id);

      AlbumDetailsViewModel albumDetailsViewModel = new AlbumDetailsViewModel();
		  albumDetailsViewModel.Albun = album;
		  albumDetailsViewModel.MyMintCollection = collectionRepository.GetMyMintCollection(GetUserId());
		  albumDetailsViewModel.MyMintHingedCollection = collectionRepository.GetMyMintHingedCollection(GetUserId());
		  albumDetailsViewModel.Used = collectionRepository.GetMyUsedCollection(GetUserId());
		  
			//Load the first page
			album.Stamps = albumRepository.GetAllStampsInAlbumFromPage(album, 0);
			

      return View(albumDetailsViewModel);
		}

		[Authorize]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(Album album)
		{
			Album newAlbum = new Album();
			newAlbum.Id = Guid.NewGuid();
			newAlbum.UserId = GetUserId();
			newAlbum.Showcased = album.Showcased;
			newAlbum.Description = album.Description;
			newAlbum.Title = album.Title;
			newAlbum.AlbumCreated = DateTime.Now;
			AlbumRepository albumRepository = new AlbumRepository();
			albumRepository.Add(newAlbum);
			albumRepository.Save();

			return RedirectToAction("Index");
		}

		[Authorize]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult StampImage(String stampId)
		{
			StampRepository stampRepository = new StampRepository();
			Stamp stamp = stampRepository.GetStamp(Guid.Parse(stampId), Guid.Empty);
			return View(stamp);
		}

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddStampToAlbum(String albumId, String stampId, String pageNumber)
    {
			if (String.IsNullOrEmpty(pageNumber))
				pageNumber = "0";
      if (String.IsNullOrEmpty(albumId) || String.IsNullOrEmpty(stampId))
      {
        return Json(new { Status = "Failed" });
      }

      Guid guidAlbumId = Guid.Parse(albumId);
      Guid guidStampId = Guid.Parse(stampId);
    	int iPageNumber = int.Parse(pageNumber);

      AlbumRepository albumRepository = new AlbumRepository();
			albumRepository.AddStampToAlbum(guidAlbumId, guidStampId, iPageNumber);
      return Json(new { Status = "Ok" }); 
    }

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult RemoveStampFromAlbum(String albumId, String stampId )
    {

      if (String.IsNullOrEmpty(albumId) || String.IsNullOrEmpty(stampId))
      {
        return Json(new { Status = "Failed" });
      }

      Guid guidAlbumId = Guid.Parse(albumId);
      Guid guidStampId = Guid.Parse(stampId);

      AlbumRepository albumRepository = new AlbumRepository();
      albumRepository.RemoveStampFromAlbum(guidAlbumId, guidStampId);

      return Json(new { Status = "Ok" }); 
    }

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddPageToAlbum(String albumId)
    {
      if (String.IsNullOrEmpty(albumId))
      {
        return Json(new { Status = "Failed" });
      }

      Guid guidAlbumId = Guid.Parse(albumId);

      AlbumRepository albumRepository = new AlbumRepository();
      albumRepository.AddPage(guidAlbumId);

      return Json(new { Status = "Ok" }); 
    }

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SavePageOrderOfAlbum(String albumId, String pageNumber, String order)
    {
      if (String.IsNullOrEmpty(albumId) || String.IsNullOrEmpty(pageNumber) || String.IsNullOrEmpty(order))
      {
        return Json(new { Status = "Failed" });
      }

      String[] orderSplit = order.Split('|');
      List<Guid> orderedList = new List<Guid>();
      foreach (var s in orderSplit)
      {
        if (String.IsNullOrEmpty(s))
          continue;

        Guid guid = Guid.Parse(s);
        orderedList.Add(guid);
      }

      AlbumRepository albumRepository = new AlbumRepository();
      albumRepository.SaveOrderOfStampsOnPage(Guid.Parse(albumId), int.Parse(pageNumber), orderedList);

      return Json(new { Status = "Ok" }); 
    }

    public ActionResult GetStampsForPage(String albumId, String pageNumber)
    {
			
			if (String.IsNullOrEmpty(albumId) || String.IsNullOrEmpty(pageNumber))
			{
				return PartialView("_AlbumPage", new AlbumPageViewModel());
			}

    	Guid gAlbumId = Guid.Parse(albumId);
    	int iPageNumber = int.Parse(pageNumber);

			//Get the album
			AlbumRepository albumRepository = new AlbumRepository();
			//For now lets get the first album
    	Album album = albumRepository.GetAlbum(gAlbumId);
			AlbumPageViewModel albumPage = new AlbumPageViewModel();
    	albumPage.Album = album;
    	albumPage.PageNumber = iPageNumber;
    	//albumPage.Stamps = albumRepository.GetAllStampsInAlbum(album).ToList();
      albumPage.Stamps = albumRepository.GetAllStampsInAlbumFromPage(album, iPageNumber).ToList();

    	return PartialView("_AlbumPage", albumPage);
    }
	}
}
